package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.AdditiveNotes;

public class AdditiveNotesPresenter extends AbstractPresenter<AdditiveNotes> {
	final String SINGLEITEM = "additiveNotes";
	final String MULTIITEM = "additiveNotess";
	
	public ArrayList<AdditiveNotes> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public AdditiveNotes tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public AdditiveNotes createItemFromJSONObject(JSONObject jsonObject){
		AdditiveNotes item = new AdditiveNotes();
		try{
			item.setId(jsonObject.getInt("additiveNoteID"));
			item.setLbs(jsonObject.getDouble("additiveNoteLbs"));
			item.setNotes(jsonObject.getString("additiveNoteNotes"));
			
			POSSupplyViewPresenter presenter = new POSSupplyViewPresenter();
			JSONObject jsonGroup = jsonObject.getJSONObject("supply");
			item.setSupply(presenter.createItemFromJSONObject(jsonGroup));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(AdditiveNotes item){
		try{
			JSONObject json = new JSONObject();
			json.put("additiveNoteID", item.getId());
			json.put("additiveNoteLbs", item.getLbs());
			json.put("additiveNoteNotes", item.getNotes());

			POSSupplyViewPresenter presenter = new POSSupplyViewPresenter();
			json.put("supply", presenter.createJSON(item.getSupply()));
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public AdditiveNotes getEmptyEntity(){
		AdditiveNotes entity = new AdditiveNotes();
		return entity;
	}
}
